package com.example.demospringmybatisplus.service;

import java.util.UUID;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import com.example.demospringmybatisplus.entity.Person;

/**
 * 自定义线程池对象工厂
 *
 * @DATE 2024/5/15
 **/
public class PersonBasePooledObjectFactory extends BasePooledObjectFactory<Person> {

    @Override
    public Person create() {
        // 创建一个新的MyObject对象
        Person myObject = new Person(UUID.randomUUID().toString());
        myObject.create();
        return myObject;
    }

    @Override
    public PooledObject<Person> wrap(Person myObject) {
        // 将MyObject对象封装到一个PooledObject对象中并返回
        return new DefaultPooledObject<>(myObject);
    }

    @Override
    public void destroyObject(PooledObject<Person> pooledObject) {
        // 销毁对象
        Person myObject = pooledObject.getObject();
        myObject.destroy();
    }

    @Override
    public boolean validateObject(PooledObject<Person> pooledObject) {
        // 验证对象是否可用
        Person myObject = pooledObject.getObject();
        return myObject.isValid();
    }
}
